package net.zoneland.knowledge.config;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * restTemplate配置.
 *
 * @author zmq
 * @date 2021/6/29 14:25
 */
@Component
@Configuration
@ConfigurationProperties(prefix = "rest.template")
public class RestTemplateConfig {

    /**
     * 线上互动url.
     */
    private String onlineInteractiveUrl;

    /**
     * 线上互动访问地址.
     */
    private String onlineInteractiveVisitUrl;

    /**
     * 线上互动访问地址url后拼接的内容.
     */
    private String onlineInteractiveVisitUrlSplit;

    /**
     * 线上互动DES加密key.
     */
    private String onlineInteractiveKey;

    /**
     * 线上互动DES加密iv.
     */
    private String onlineInteractiveIv;
    /**
     * 线上互动admin.
     */
    private String onlineInteractiveAdmin;
    /**
     * 线上互动pwd.
     */
    private String onlineInteractivePwd;

    /**
     * rest连接超时时间（单位为ms）.
     */
    private int connectTimeout = 3000;

    /**
     * 获取响应超时时间（单位为ms）。
     */
    private int readTimeout = 6000;

    /**
     * rest模板.
     *
     * @param requestFactory 请求工厂
     * @return rest模板
     */
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory requestFactory) {
        // 添加转换器
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
        messageConverters.add(new FormHttpMessageConverter());
        RestTemplate restTemplate = new RestTemplate(messageConverters);
        restTemplate.setRequestFactory(requestFactory);
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
        return restTemplate;
    }

    /**
     * rest工厂。
     *
     * @return 请求工厂
     */
    @Bean
    public ClientHttpRequestFactory requestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(readTimeout);
        factory.setConnectTimeout(connectTimeout);
        return factory;
    }

    /**
     * json转换类.
     *
     * @return
     */
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        return objectMapper;
    }

    public String getOnlineInteractiveUrl() {
        return onlineInteractiveUrl;
    }

    public void setOnlineInteractiveUrl(String onlineInteractiveUrl) {
        this.onlineInteractiveUrl = onlineInteractiveUrl;
    }

    public String getOnlineInteractiveKey() {
        return onlineInteractiveKey;
    }

    public void setOnlineInteractiveKey(String onlineInteractiveKey) {
        this.onlineInteractiveKey = onlineInteractiveKey;
    }

    public String getOnlineInteractiveIv() {
        return onlineInteractiveIv;
    }

    public void setOnlineInteractiveIv(String onlineInteractiveIv) {
        this.onlineInteractiveIv = onlineInteractiveIv;
    }

    public String getOnlineInteractiveAdmin() {
        return onlineInteractiveAdmin;
    }

    public void setOnlineInteractiveAdmin(String onlineInteractiveAdmin) {
        this.onlineInteractiveAdmin = onlineInteractiveAdmin;
    }

    public String getOnlineInteractivePwd() {
        return onlineInteractivePwd;
    }

    public void setOnlineInteractivePwd(String onlineInteractivePwd) {
        this.onlineInteractivePwd = onlineInteractivePwd;
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public int getReadTimeout() {
        return readTimeout;
    }

    public void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }

    /**
     * @return the onlineInteractiveVisitUrl
     */
    public String getOnlineInteractiveVisitUrl() {
        return onlineInteractiveVisitUrl;
    }

    /**
     * @param onlineInteractiveVisitUrl to set
     */
    public void setOnlineInteractiveVisitUrl(final String onlineInteractiveVisitUrl) {
        this.onlineInteractiveVisitUrl = onlineInteractiveVisitUrl;
    }

    /**
     * @return the onlineInteractiveVisitUrlSplit
     */
    public String getOnlineInteractiveVisitUrlSplit() {
        return onlineInteractiveVisitUrlSplit;
    }

    /**
     * @param onlineInteractiveVisitUrlSplit to set
     */
    public void setOnlineInteractiveVisitUrlSplit(final String onlineInteractiveVisitUrlSplit) {
        this.onlineInteractiveVisitUrlSplit = onlineInteractiveVisitUrlSplit;
    }
}
